import unittest
from api.login_api import LoginApi
from api.login_api import EmployeeApi
import app
import parameterized

from utils import read_data


class EmpTestPara(unittest.TestCase):
    """员工类"""

    @classmethod
    def setUpClass(cls):
        """实例化登入、员工模块"""
        cls.ihrm_emp_api = EmployeeApi()
        cls.ihrm_login_api = LoginApi()

    filename = read_data(app.BASE_dir + '/data/employee.json')

    @parameterized.parameterized.expand(filename)
    def test01_manger_emp(self, case, login_data, add_emp, modify_emp, common_response_code, common_success
                          , common_code, common_message):
        """员工增删改查模块"""
        # print(case)
        # 登入获取令牌
        response = self.ihrm_login_api.login(login_data)
        token = "Bearer " + response.json().get('data')
        app.HEADERS['Authorization'] = token

        # 添加员工
        response = self.ihrm_emp_api.add_emp(app.HEADERS, add_emp)
        print("添加员工信息为：", response.json())
        # 添加员工断言
        self.assertEqual(common_response_code, response.status_code)
        self.assertEqual(common_success, response.json().get('success'))
        self.assertEqual(common_code, response.json().get('code'))
        self.assertIn(common_message, response.json().get('message'))

        # 获取员工ID
        emp_id = response.json().get('data').get('id')

        # 查询员工
        response = self.ihrm_emp_api.check_emp(app.HEADERS, emp_id)
        print("查询员工为：", response.json())
        # 查询员工断言
        self.assertEqual(common_response_code, response.status_code)
        self.assertEqual(common_success, response.json().get('success'))
        self.assertEqual(common_code, response.json().get('code'))
        self.assertIn(common_message, response.json().get('message'))

        # 修改员工
        response = self.ihrm_emp_api.update_emp(app.HEADERS, modify_emp, emp_id)
        print("修改员工为：", response.json())
        # 修改员工断言
        self.assertEqual(common_response_code, response.status_code)
        self.assertEqual(common_success, response.json().get('success'))
        self.assertEqual(common_code, response.json().get('code'))
        self.assertIn(common_message, response.json().get('message'))

        # 删除员工
        response = self.ihrm_emp_api.del_emp(app.HEADERS, emp_id)
        print("删除员工为：", response.json())
        # 删除员工断言
        self.assertEqual(common_response_code, response.status_code)
        self.assertEqual(common_success, response.json().get('success'))
        self.assertEqual(common_code, response.json().get('code'))
        self.assertIn(common_message, response.json().get('message'))

        # 查询所有员工
        response = self.ihrm_emp_api.check_all_emp(app.HEADERS)
        print("查询所有员工为：", response.json())
        # 查询所有员工断言
        self.assertEqual(common_response_code, response.status_code)
        self.assertEqual(common_success, response.json().get('success'))
        self.assertEqual(common_code, response.json().get('code'))
        self.assertIn(common_message, response.json().get('message'))
